package com.foxmail.aroundme.boring.utils.net;

import com.foxmail.aroundme.boring.BuildConfig;
import com.foxmail.aroundme.boring.utils.Config;
import com.foxmail.aroundme.boring.utils.URL;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by gzl on 12/18/16.
 */

public class RetrofitUtil {

    private volatile Retrofit retrofit;

    private final int connectTimeOut = 10;

    private final int readTimeOut = 20;

    private static RetrofitUtil retrofitUtil;


    private RetrofitUtil() {
        //创建对象
        OkHttpClient okHttpClient = null;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        //日志打印
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(logging);
        }

        okHttpClient = builder.connectTimeout(connectTimeOut, TimeUnit.SECONDS)
                        .readTimeout(readTimeOut, TimeUnit.SECONDS)
                        .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(URL.JokeURL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient)
                .build();
    }


    public static RetrofitUtil newsInstance() {

        if (retrofitUtil == null) {

            synchronized (RetrofitUtil.class) {
                if (retrofitUtil == null) {
                    retrofitUtil = new RetrofitUtil();
                }
            }
        }

        return retrofitUtil;
    }

    public Retrofit getRetrofit() {

        return retrofit;

    }
}
